package com.jiyun.controller;

import com.jiyun.entity.Class;
import com.jiyun.entity.Grade;
import com.jiyun.entity.Student;
import com.jiyun.service.StudentService;
import com.jiyun.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("studentController")
public class StudentController {

    @Autowired
    StudentService ss;


    @RequestMapping("index")
    public  String index(HttpServletRequest request){
        List<StudentVo> studentVoList = ss.findAll2();
        request.setAttribute("studentList",studentVoList);
        request.setAttribute("helloword","helloword");


        return "index";
    }


    @RequestMapping("tostudent")
    public  Object tostuent(HttpServletRequest request){

        return ss.findAll();
    }
    @RequestMapping("findAll")
    public  Object findAll(){

        return ss.findAll2();
    }
    @RequestMapping("toadd")
    public  String toadd(HttpServletRequest request){
        List<Class> classList = ss.findClass();
        List<Grade> gradeList = ss.findGrade();
        request.setAttribute("gradeList",gradeList);
        request.setAttribute("classList",classList);
        return "add";
    }
    @RequestMapping("toEdit")
    public  String toEdit(Integer id,HttpServletRequest request){
        List<Class> classList = ss.findClass();
        List<Grade> gradeList = ss.findGrade();
        request.setAttribute("gradeList",gradeList);
        request.setAttribute("classList",classList);
        request.setAttribute("stu",ss.findstudentById(id));
        return "edit";
    }
    @RequestMapping("del")
    public  String del(Integer id,HttpServletRequest request){

        ss.delete(id);
        return "redirect:index";
    }
    @RequestMapping("add")
    public  String add(Student student){

        ss.inster(student);
        return "redirect:index";
    }
    @RequestMapping("edit")
    public  String edit(Student student){

        ss.update(student);
        return "redirect:index";
    }

    public void inster(Student student){
        ss.inster(student);
    }
    public void delete(int id){
        ss.delete(id);
    }
    public void update(Student student){
        ss.update(student);
    }

}
